package com.graphite;

import com.alibaba.fastjson.JSON;
import com.graphite.client.GraphiteNettyClient;
import com.graphite.model.GraphiteMetric;
import com.graphite.model.GraphiteRequest;
import com.graphite.model.GraphiteResponse;
import com.graphite.task.FetchGraphiteDataTaskExector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

/**
 * @projectName: graphite-connector-java
 * @packageName: com.graphite
 * @className: com.graphite-> GraphiteApplication
 * @createDate: 2021-06-05 12:12
 * @author: ZhangSheng
 * @version: 1.0
 * @description:
 */
public class GraphiteApplicationTest {

    private static final Logger logger = LoggerFactory.getLogger(GraphiteApplicationTest.class);

    private static final  GraphiteNettyClient<GraphiteRequest,GraphiteResponse> client = new GraphiteNettyClient<GraphiteRequest,GraphiteResponse>();

    private FetchGraphiteDataTaskExector<GraphiteRequest,GraphiteResponse> exector = new FetchGraphiteDataTaskExector<GraphiteRequest, GraphiteResponse>();
    public static void main(String[] args){
        timerSendMessage();
        timerGetMessage();
    }
    public static void timerGetMessage(){
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                getMessage();
            }
        };
        timer.schedule(task,0,1000);
    }
    public static void getMessage(){
        GraphiteRequest request = new GraphiteRequest();
        request.setFormat("json");
        request.setTarget("foo.bar");
        request.setConvertClass(GraphiteResponse.class);
        request.setFrom("-5minute");
        request.setEndpoint("http://192.168.26.128:80/render");
        GraphiteResponse graphiteResponse = null;
        try {
            graphiteResponse = client.fetchData(request);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        logger.info("获取到的数据：{}", JSON.toJSONString(graphiteResponse));
    }

    private static void timerSendMessage(){
        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                try {
                    try {
                        sendMessage();
                    } catch (TimeoutException e) {
                        e.printStackTrace();
                    } catch (ExecutionException e) {
                        e.printStackTrace();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        timer.schedule(timerTask,1000L,1000L);
    }


    public static  void  sendMessage() throws InterruptedException, TimeoutException, ExecutionException {
        List<GraphiteMetric> graphiteMetrics = new ArrayList<GraphiteMetric>();
        GraphiteMetric metric = new GraphiteMetric();
        metric.setMetricName("foo.bar");
        metric.setTime(System.currentTimeMillis()/1000);
        metric.setValue(Double.valueOf(Math.floor(new Random().nextDouble()*10)));
        graphiteMetrics.add(metric);
        System.out.println("开始发送数据：");
        boolean b = client.sendData("192.168.26.128", 2003, graphiteMetrics);
        System.out.println((b==true?"============>发送数据成功！":"============>发送数据失败!"));
    }


}
